export default {
  data() {
    return {
			formItemReset: {}, // 重置备份
      // 点击查询后搜索条件才更新
      cacheSearchData: {},
      // 表格选中行的数组
      tSelectedAry: []
    }
  },
	created() {
		this.formItemReset = JSON.parse(JSON.stringify(this.formItem))
	},
  methods: {
    // 重置
    resetClickFn() {
			this.formItem = JSON.parse(JSON.stringify(this.formItemReset))
    },
    // 查询
    formSearch() {
      this.tableConfig.pageNo = 1
      let tmp = Object.assign({}, this.formItem)
      if (typeof this.modifySearchData === 'function') {
        tmp = this.modifySearchData(tmp)
      }
      if (!tmp) return
      this.cacheSearchData = tmp
      this.getTableData()
    },
    // 请求表格数据
    getTableData() {
      let requestData = Object.assign(
        {},
        {
          page: {
						current: this.tableConfig.pageNo,
          	size: this.tableConfig.pageSize,
					}
        },
        this.cacheSearchData
      )
      this.$usePost(this.requestUrls.query, requestData).then((res) => {
				this.tableConfig.tData = res?.data?.records|| []
				this.tableConfig.total = Number(res?.data?.total)
        // 如果有额外的处理，则使用getTableDataCallBack
        if (typeof this.getTableDataCallBack === 'function') {
          this.getTableDataCallBack(res)
        }
      })
    },
    // 页码改变的回调
    currentChange(pageNo) {
      this.tableConfig.pageNo = pageNo
      this.getTableData()
    },
    // 切换每页条数时的回调
    pageSizeChange(pageSize) {
      this.tableConfig.pageNo = 1
      this.tableConfig.pageSize = pageSize
      this.getTableData()
    },
    // 选中项发生变化的回调
    selectionChange(selection, index) {
      this.tSelectedAry = selection
      // console.log(this.tSelectedAry, 'this.tSelectedAry')
    },
    
    /**
     * 点击删除
     * @param {*} e // 事件本身
     * @param {*} tSelectedAry // 选中的数组名名称
     * @param {*} tableConfig // 表格配置名称
     */
    delClickFn(
      e,
      tSelectedAry = 'tSelectedAry',
      tableConfig = 'tableConfig'
    ) {
      if (this[tSelectedAry].length) {
				this.$hMsgBox.confirm({
					title: "确定要删除所选数据？",
					content: "",
					onOk: () => {
						this.comfirmDelete()
					}
				})
      } else {
        this.$public.message(this, 'error', '请选择需要删除的数据！')
      }
    },
    /**
     * 确认删除
     * @param {*} deleteUrl // 删除链接
     * @param {*} deleteId  // 获取的字段名称
     * @param {*} sendFieldName  // 传参字段名称
     * @param {*} tSelectedAry // 数组名称
     */
    comfirmDelete(config = {}) {
      const {
        deleteUrl = this.requestUrls.delete,
        deleteId = 'id',
        sendFieldName = 'ids',
        tSelectedAry = 'tSelectedAry'
      } = this.deleteConfig || {}
      let delConfigAry = []
      this[tSelectedAry].forEach((item, i) => {
        let v
        if (typeof deleteId === 'string') {
          v = item[deleteId]
        } else {
          v = {}
          deleteId.map((j) => {
            // console.log(item,j,item[j])
            v[j] = item[j]
          })
        }
        delConfigAry[i] = v
      })
      let params = {}
      params[sendFieldName] = delConfigAry
      this.$usePost(deleteUrl, params).then((res) => {
        this.formSearch()
        delConfigAry = []
        this[tSelectedAry] = []
        this.$public.message(this, 'success', '操作成功')
      })
    },
		// 单条删除
		deleteSingle(row) {
			this.$hMsgBox.confirm({
				title: "确定要删除该条数据？",
				content: "",
				onOk: () => {
					let sendFieldName = this.deleteConfig['sendFieldName']
					let params = {}
      		params[sendFieldName] = [row[this.deleteConfig['deleteId']]]
					this.$usePost(this.requestUrls.delete, params).then((res) => {
						this.formSearch()
						this.$public.message(this, 'success', '操作成功')
					})
				}
			})
		},
  },
}
